package com.woniuxy.carlife.controller;


import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.dto.ServiceCategoryDto;
import com.woniuxy.carlife.param.SaveServiceCategoryParam;
import com.woniuxy.carlife.service.ServiceCategoryService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fx
 * @ClassName:ServiceCategoryController
 * @Description: 服务类别控制类
 * @date 2021/4/6 20:00
 **/

@RestController
@RequestMapping("/serviceCategory")
@Api(tags = "服务类别接口信息")
@Slf4j
public class ServiceCategoryController {

    @Resource
    private ServiceCategoryService service;

    /**
     * 功能描述〈添加服务类别〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/10 16:17
     * @throws
     */
    @ApiOperation(value = "添加服务类别",notes = "<span style='color:red;'>添加服务类别，主键参数不用传入</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "添加服务类别成功"),
            @ApiResponse(code=20001,message = "添加服务类别失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @PostMapping("/platformmanage/servicecategory")
    public Result saveServiceCatrgory(@RequestBody @ApiParam SaveServiceCategoryParam categoryParam){
        log.info("saveServiceCatrgory开始:参数：{}",categoryParam);
        //判断传入参数
        if (categoryParam.getServiceCategoryName().trim().length()==0||
            categoryParam.getServiceCategoryLevel()==null||
            categoryParam.getServiceCategoryLevel()<0||
            categoryParam.getServiceCategorySuperior()==null||
            categoryParam.getServiceCategorySuperior()<0){
            log.info("saveServiceCatrgory结束:{}","传入参数错误");
            return new Result(false, StatusCode.PARAMERROR,"传入参数错误");
        }
        int i = service.saveServiceCatrgory(categoryParam);
        if (i==1){
            log.info("saveServiceCatrgory结束:返回：{}","添加服务类别成功");
            return new Result(true, StatusCode.OK,"添加服务类别成功");
        }else if (i==0){
            log.info("saveServiceCatrgory结束:返回：{}","添加服务类别失败");
            return new Result(false, StatusCode.ERROR,"添加服务类别失败");
        }else{
            log.info("saveServiceCatrgory结束:返回：{}","当前类别已存在");
            return new Result(false, StatusCode.ERROR,"当前类别已存在");
        }
    }

    /**
     * 功能描述〈查询所有类别状态为已使用的,并按类别等级分类关联〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/10 17:09
     * @throws
     */
    @ApiOperation(value = "查询所有类别状态为已使用的,并按类别等级分类关联",notes = "<span style='color:red;'>没有参数</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询类别商品成功",response = ServiceCategoryDto.class),
            @ApiResponse(code=20001,message = "查询类别商品失败")
    })
    @GetMapping("/merchantmanage/servicecategorystatus")
    public Result findAllServiceCategory(){
        log.info("saveServiceCatrgory开始:");
        List<ServiceCategoryDto> allServiceCategory = service.findAllServiceCategory();
        log.info("saveServiceCatrgory结束:返回：{}",allServiceCategory);
        return new Result(true, StatusCode.OK,"查询类别商品成功",allServiceCategory);
    }

    /**
     * 功能描述〈根据id修改类别状态为停用〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/12 9:56
     * @throws
     */
    @ApiOperation(value = "根据id修改类别状态为停用",notes = "<span style='color:red;'>根据id修改类别状态为停用</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "停用商品类别成功"),
            @ApiResponse(code=20001,message = "停用商品类别失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryId",value = "类别id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @PutMapping("/platformmanage/servicecategorystatus")
    public Result updateServiceCategoryStatusByCategoryId(Integer categoryId){
        log.info("saveServiceCatrgory开始:参数:{}",categoryId);
        if (categoryId==null||categoryId<=0){
            log.info("saveServiceCatrgory结束:返回:{}","传入参数为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"传入参数错误");
        }
        boolean b = service.updateServiceCategoryStatusByCategoryId(categoryId);
        if (b){
            log.info("saveServiceCatrgory结束:返回:{}","停用商品类别成功");
            return new Result(true, StatusCode.OK,"停用商品类别成功");
        }
        log.info("saveServiceCatrgory结束:返回:{}","停用商品类别失败");
        return new Result(false, StatusCode.ERROR,"停用商品类别失败");
    }
}

